package com.telnetnz.ticketdesk.common.config;

import java.io.InputStream;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class PageConfig
{

	private static Logger logger = Logger.getLogger(PageConfig.class);
	private static final HashMap<String, String> configs;

	static
	{
		configs = new HashMap<String, String>();
		try
		{
			InputStream is = PageConfig.class.getResourceAsStream("/page-config.xml");
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			InputSource isc = new InputSource(is);
			Document doc = db.parse(isc);

			// 初始化静态配置变量
			NodeList nodeList = doc.getElementsByTagName("constant");
			for (int i = 0; i < nodeList.getLength(); i++)
			{
				Node node = nodeList.item(i);
				if (node.getNodeType() == Node.ELEMENT_NODE)
				{
					Element element = (Element) node;
					configs.put(element.getAttribute("name"), element.getAttribute("value"));
				}
			}
		}
		catch (Exception e)
		{
			logger.info("Unknown Exception");
			logger.debug(e.getMessage(), e);
		}
	}

	private PageConfig()
	{
	}

	public static HashMap<String, String> getConfigs()
	{
		return configs;
	}

	public static String getConfig(String con_name)
	{
		return configs.get(con_name);
	}
}
